package cn.stylefeng.guns.modular.yingbao.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.yingbao.entity.VipPay;
import cn.stylefeng.guns.modular.yingbao.mapper.VipPayMapper;
import cn.stylefeng.guns.modular.yingbao.model.params.VipPayParam;
import cn.stylefeng.guns.modular.yingbao.model.result.VipPayResult;
import  cn.stylefeng.guns.modular.yingbao.service.VipPayService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  会员收费表 服务实现类
 * </p>
 *
 * @author zs
 * @since 2019-12-16
 */
@Service
public class VipPayServiceImpl extends ServiceImpl<VipPayMapper, VipPay> implements VipPayService {

    @Override
    public void add(VipPayParam param){
        VipPay entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(VipPayParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(VipPayParam param){
        VipPay oldEntity = getOldEntity(param);
        VipPay newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public VipPayResult findBySpec(VipPayParam param){
        return null;
    }

    @Override
    public List<VipPayResult> findListBySpec(VipPayParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(VipPayParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public ResponseData queryAll() {
        List<VipPay> vipPays = this.baseMapper.selectList(new QueryWrapper<VipPay>());
        if(vipPays.size() >0){
            return ResponseData.success(vipPays);
        }
        return ResponseData.error(500,"暂无数据");
    }

    @Override
    public ResponseData queryById(String id) {
        VipPay vipPay = this.baseMapper.selectById(id);
        if(vipPay != null){
            return ResponseData.success(vipPay);
        }
        return ResponseData.error(500,"失败");
    }

    private Serializable getKey(VipPayParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private VipPay getOldEntity(VipPayParam param) {
        return this.getById(getKey(param));
    }

    private VipPay getEntity(VipPayParam param) {
        VipPay entity = new VipPay();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
